package com.ruoyi.baoziyi.controller;

import com.ruoyi.baoziyi.domain.SelectBZYImgInfo;
import com.ruoyi.baoziyi.domain.SerBaoziyiDeviceInfo;
import com.ruoyi.baoziyi.domain.SerBaoziyiImgInfo;
import com.ruoyi.baoziyi.service.BZYDeviceService;
import com.ruoyi.baoziyi.service.BZYImgService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.permission.Dao.SerDevicePermi;
import com.ruoyi.permission.domain.BaseBlockPermiDeviceInfo;
import com.ruoyi.permission.domain.BlockPermiDeviceInfo;
import com.ruoyi.permission.mapper.SerUnitUserMapper;
import com.ruoyi.permission.service.PermiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

@Api("BZY")
@Slf4j
@RestController
@RequestMapping("/BZY")
public class BZYController extends BaseController {

    @Autowired
    private BZYImgService bzyImgService;

    @Autowired
    private BZYDeviceService bzyDeviceService;

    @Autowired
    private SerUnitUserMapper unitUserMapper;

    @Autowired
    private PermiService permiService;


    @ApiOperation("拉取ftp服务器上的图片和设备信息")
    @GetMapping("/getFromFtpServer")
    public AjaxResult getFromFtpServer() throws ParseException {
        int bzyDevice = bzyDeviceService.getBZYDeviceInfo();
        int bzyImage = bzyImgService.getBZYImageInfo();
        if(bzyDevice !=0 && bzyImage!=0){
            return AjaxResult.success("获取设备及图片成功");
        }else {
            return AjaxResult.error("设备或图片已存在");
        }
    }
    @ApiOperation("获取设备列表")
    @GetMapping("/getBZYDeviceList")
    public AjaxResult getDeviceList(){
        Long userIdL = SecurityUtils.getLoginUser().getUser().getUserId();
        Integer userId=Integer.valueOf(userIdL.intValue());
        Integer unitId = 0;
        if(userId != 1 && userId != 20) {
            unitId = unitUserMapper.selectByPrimaryKey(userId).getUnitId();
        }
        //管理员用户
        if(unitId == 0){
            List<SerDevicePermi> serDevicePermiList = new ArrayList<>();
            List<SerBaoziyiDeviceInfo> baoZiYiDevicePermiList = bzyDeviceService.selectAllBZYDeviceInfoByCondition(serDevicePermiList);
            if(!baoZiYiDevicePermiList.isEmpty()) {
                return AjaxResult.success(baoZiYiDevicePermiList);
            }else{
                return AjaxResult.error("数据表中无设备");
            }
        }else {
            //获取单位下所有设备
            List<BaseBlockPermiDeviceInfo> baseBlockPermiDeviceInfoList = permiService.selectAllDeviceByUnitId(unitId);
            List<SerDevicePermi> baoZiYiDevicePermiList = new ArrayList<>();
            for(BaseBlockPermiDeviceInfo baseBlockPermiDeviceInfo:baseBlockPermiDeviceInfoList) {
                if(baseBlockPermiDeviceInfo.getTypeId() == 7){
                    SerDevicePermi serDevicePermi = new SerDevicePermi().setDeviceName(baseBlockPermiDeviceInfo.getDeviceName()).setDeviceNo(baseBlockPermiDeviceInfo.getDeviceId())
                            .setDeviceType(baseBlockPermiDeviceInfo.getDeviceType()).setDeviceSerial(baseBlockPermiDeviceInfo.getDeviceSerial()).setId(baseBlockPermiDeviceInfo.getId());
                    baoZiYiDevicePermiList.add(serDevicePermi);
                }
            }
            if(!baoZiYiDevicePermiList.isEmpty()) {
                return AjaxResult.success(baoZiYiDevicePermiList);
            }else{
                return AjaxResult.error("数据表中无设备");
            }
        }
    }

    @ApiOperation("根据条件获取设备")
    @GetMapping("/getBZYDeviceByCondition")
    public AjaxResult getBZYDeviceByCondition(SerBaoziyiDeviceInfo serBaoziyiDeviceInfo){
        if(bzyDeviceService.selectBZYDeviceByCondition(serBaoziyiDeviceInfo)!=null){
            return AjaxResult.success(serBaoziyiDeviceInfo);
        }else{
            return AjaxResult.error("数据表中没有符合条件的设备");
        }
    }

    @ApiOperation("根据设备的deviceId更新设备信息")
    @PostMapping("/updateBZYDeviceInfo")
    public AjaxResult updateBZYDeviceInfo(SerBaoziyiDeviceInfo serBaoziyiDeviceInfo) throws IOException {
        int i = bzyDeviceService.updateBZYDeviceInfo(serBaoziyiDeviceInfo);
        if(i>0){
            return AjaxResult.success("修改成功");
        }else {
            return AjaxResult.error("修改失败");
        }
    }

    @ApiOperation("分页拉取图片")
    @GetMapping("/getPhotoList")
    public TableDataInfo getBZYPhotoByDate(SelectBZYImgInfo selectBZYImgInfo){
        startPage();
        try {
            List<SerBaoziyiImgInfo> BZYPhotoList = bzyImgService.selectBZYPhotoList(selectBZYImgInfo);
            return getDataTable(BZYPhotoList);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @ApiOperation("根据图片id删除图片")
    @PostMapping("/deletePhoto")
    public AjaxResult deletePhotoById(long id){
        int i = bzyImgService.deletePhotoById(id);
        if(i>0){
            return AjaxResult.success("删除成功");
        }else {
            return AjaxResult.error("删除失败");
        }
    }


}
